package com.ruoyi.web.controller.module.estate;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.estate.EstateCallCenter;
import com.ruoyi.system.service.module.estate.IEstateCallCenterService;
import com.ruoyi.system.service.module.estate.IEstateService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 小区的呼叫中心列Controller
 *
 * @author ruoyi
 * @date 2024-03-02
 */
@Validated
@Api(value = "小区的呼叫中心列控制器", tags = {"小区的呼叫中心列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/estate/call/center")
public class EstateCallCenterController extends BaseController {

    private final IEstateCallCenterService estateCallCenterService;
	private final IEstateService estateService;

    /**
     * 查询小区的呼叫中心列列表
     */
    @ApiOperation("查询小区的呼叫中心列列表")
    @PreAuthorize("@ss.hasPermi('module:callCenter:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateCallCenter> list(@Validated EstateCallCenter estateCallCenter) {
        QueryWrapper<EstateCallCenter> qw = new QueryWrapper<>();
		if(StrUtil.isNotEmpty(estateCallCenter.getSerialNo())){
			qw.like("serial_no", estateCallCenter.getSerialNo());
		}
		if(estateCallCenter.getEstateId()!=null){
			qw.eq("estate_id", estateCallCenter.getEstateId());
		}
        TableDataInfo<EstateCallCenter> td = PageUtils.buildDataInfo(estateCallCenterService.page(PageUtils.buildPage(),qw));
        List<EstateCallCenter> list = td.getRows();
        return td;
    }

    /**
     * 导出小区的呼叫中心列列表
     */
    @ApiOperation("导出小区的呼叫中心列列表")
    @PreAuthorize("@ss.hasPermi('module:callCenter:export')")
    @Log(title = "小区的呼叫中心列", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateCallCenter> export(@Validated EstateCallCenter estateCallCenter) {
        List<EstateCallCenter> list = estateCallCenterService.list();
        ExcelUtil<EstateCallCenter> util = new ExcelUtil<EstateCallCenter>(EstateCallCenter.class);
        return util.exportExcel(list, "小区的呼叫中心列");
    }

    /**
     * 获取小区的呼叫中心列详细信息
     */
    @ApiOperation("获取小区的呼叫中心列详细信息")
    @PreAuthorize("@ss.hasPermi('module:callCenter:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateCallCenter> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateCallCenter estateCallCenter = estateCallCenterService.getById(id);
        return AjaxResult.success(estateCallCenter);
    }


	/**
	 * 获取小区的呼叫中心列详细信息
	 */
	@ApiOperation("获取小区的呼叫中心列详细信息")
//	@PreAuthorize("@ss.hasPermi('module:callCenter:query')")
	@GetMapping("/getBySysUserId")
	public AjaxResult getBySysUserId() {
		SysUser sysUser = getUser();
		QueryWrapper<EstateCallCenter> qw = new QueryWrapper<>();
//		qw.eq("sys_user_id", sysUser.getUserId());
		if(sysUser.getEstateId()!=null){
			qw.eq("estate_id",sysUser.getEstateId());
		}
		qw.eq("state", 1);
		qw.last("limit 1");
		EstateCallCenter estateCallCenter = estateCallCenterService.getOne(qw);
		if(estateCallCenter != null){
			return AjaxResult.success(estateCallCenter);
		}
		return AjaxResult.success();
	}

    /**
     * 新增小区的呼叫中心列
     */
    @ApiOperation("新增小区的呼叫中心列")
    @PreAuthorize("@ss.hasPermi('module:callCenter:add')")
    @Log(title = "小区的呼叫中心列", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateCallCenter estateCallCenter) {
		if(estateCallCenter.getEstateId()!= null){
			Estate estate = estateService.findById(estateCallCenter.getEstateId());
			if(estate != null){
				estateCallCenter.setEstateName(estate.getEstateName());
			}
		}
		estateCallCenter.setOnlineState(0);
        return toAjax(estateCallCenterService.save(estateCallCenter) ? 1 : 0);
    }

    /**
     * 修改小区的呼叫中心列
     */
    @ApiOperation("修改小区的呼叫中心列")
    @PreAuthorize("@ss.hasPermi('module:callCenter:edit')")
    @Log(title = "小区的呼叫中心列", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateCallCenter estateCallCenter) {
		if(estateCallCenter.getEstateId()!= null){
			Estate estate = estateService.findById(estateCallCenter.getEstateId());
			if(estate != null){
				estateCallCenter.setEstateName(estate.getEstateName());
			}
		}
        return toAjax(estateCallCenterService.updateById(estateCallCenter) ? 1 : 0);
    }

    /**
     * 删除小区的呼叫中心列
     */
    @ApiOperation("删除小区的呼叫中心列")
    @PreAuthorize("@ss.hasPermi('module:callCenter:remove')")
    @Log(title = "小区的呼叫中心列" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateCallCenterService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
